clear all;clc
addpath(genpath('.'));  %添加工具箱路径
%% 超参数设置
% ZOOM=256;%暂定256*256为一个分区
N=128;%暂定每个分区中的块大小为：128*128分块
PSNR=[];SSIM=[];
% for a=34.5:3:40.5
a=34.5;  %33~34.5 OK
b=a-1;
%% 读入图像
Cover=rgb2ycbcr(imread('./Image/image_8.png'));
Cover=imcrop(Cover,[1,1,2559,1535]);
% [DFT_Cover,P]=InputProc(Cover);%取幅值谱
% Cover=imresize(Cover,2);
Y_Cover=Cover(:,:,1);
Orgin_Y_Cover=Y_Cover;
[w,h]=size(Y_Cover);
%% 获取图像的边缘框--嵌同步水印
% up=Y_Cover(1:N,N:h-N+1);
% down=Y_Cover(w-N+1:w,N:h-N+1);
% left=Y_Cover(N:w-N+1,1:N);
% right=Y_Cover(N:w-N+1,h-N+1:h);
up=Y_Cover(1:N,N+1:h-N);
down=Y_Cover(w-N+1:w,N+1:h-N);
left=Y_Cover(N+1:w-N,1:N);
right=Y_Cover(N+1:w-N,h-N+1:h);
Frame_Set={up,down,left,right};

%% 获取图像内部区域--嵌认证水印
Orgin_ZOOM=Y_Cover(N+1:w-N,N+1:h-N);
ZOOM=Orgin_ZOOM;
%% 嵌同步水印--波峰波谷生成
count=1;
for data =Frame_Set
    data=data{1};
    [row,col]=size(data);
    for i=1:row/N
        for j=1:col/N
            Cover_block=data((i-1)*N+1:i*N,(j-1)*N+1:j*N);
            if count==1|| count==2
                [Stego_block,intensity]=Generate_correspon_A(Cover_block,a,b);%1,3象限
            end
            if count==3 || count==4
                [Stego_block,intensity]=Generate_correspon_B(Cover_block,a,b);%2,4象限
            end
%             PSNR=find_psnr(Cover_block,Stego_block);
            Y_stego((i-1)*N+1:i*N,(j-1)*N+1:j*N)=Stego_block;
        end
    end
    if count==1
        Y_Cover(1:N,N:size(Y_stego,2)+N-1)=Y_stego;
    end
    if count==2
        Y_Cover(w-N+1:w,N:size(Y_stego,2)+N-1)=Y_stego;
    end
    if count==3
        Y_Cover(N:size(Y_stego,1)+N-1,1:N)=Y_stego;
    end
    if count==4
        Y_Cover(N:size(Y_stego,1)+N-1,h-N+1:h)=Y_stego;
    end
    count=count+1;
    Y_stego=[];
end

%% 嵌入认证水印--利用他人的方案
[row_z,col_z]=size(ZOOM);
WN=2*N;%
bit_length=64;%256 标配 64 bit
msg=repmat([1,0],1,32);
% p=1;    %指向msg的指针
% 固定bit序列
%% 1、Zhang

%% 2、Lin
% load WZ
% load g
% for I=1:row_z/WN
%     for J=1:col_z/WN
%            ZOOM_Block=ZOOM((I-1)*WN+1:I*WN,(J-1)*WN+1:J*WN);
%            %Zhang Scheme
% %            Stego_Block=Zhang_alg(ZOOM_Block,88,0.01,msg);%0.00005太小，0.0005，0.002 不可提取
% %            ZOOM((I-1)*WN+1:I*WN,(J-1)*WN+1:J*WN)=Stego_Block;    
%            %Lin Scheme
%            [img_embed,fudu_0,fudu_1,flag,index_num1,margin1_data,flag_7,flag_matr,d_r,threshold]=Linembed(WZ,ZOOM_Block);%嵌入水印
%            ZOOM((I-1)*WN+1:I*WN,(J-1)*WN+1:J*WN)=img_embed; 
%     end
% end
%% 3、Fang
load W
ZOOM=Embedding_test(ZOOM,W);
%% 嵌入程序

%% 拼接同步载体和认证载体
Y_Cover(N+1:w-N,N+1:h-N)=ZOOM;
PSNR=[PSNR;find_psnr(Y_Cover,Orgin_Y_Cover)];
SSIM=[SSIM;ssim(Y_Cover,Orgin_Y_Cover)];
%% 转回RGB
Stego=cat(3,Y_Cover,Cover(:,:,2),Cover(:,:,3));
Stego=ycbcr2rgb(Stego);
%% 存起来
imwrite(Stego,['./Embed_image/Fang/Marked_image1_8' '_' num2str(a) '.png']);
% end
save Stego Stego

% My_BCHdec
% My_BCHenc
%% 秘密信息生成
% key=rng; %%key为伪随机序列种子
% msg = num2str(uint8(rand(1,w*h)));
% msg=strrep(msg,' ','');%剔除空格
% p=1;    %指向msg的指针
%% 秘密信息生成--注：仅仅生成35bit
%获取秘密信息,一次取35位，前11位走BCH(31,11),后24位走BCH(63,24)
%进行BCH编码
% msg_A=repmat(My_BCHenc(emd_msg(1:11),31,11),1,2);
% msg_B=My_BCHenc(emd_msg(12:end),63,24);
%% 嵌入
% 前两层For循环为了获取区域，后两层For循环为了嵌入
% for I=1:w/ZOOM
%     for J=1:h/ZOOM
%           C_ZOOM=Y_Cover((I-1)*ZOOM+1:I*ZOOM,(J-1)*ZOOM+1:J*ZOOM);%取出这个ZOOM
%           [sub_w,sub_h]=size(C_ZOOM);
%         for i=1:sub_w/N 
%            for j=1:sub_h/N
%                % 取块，且块间间隔为一个ZOOM-N
%                 Cover_block=C_ZOOM((i-1)*N+1:i*N,(j-1)*N+1:j*N);
%                 if i==1 && j==1 %确定为信息块
%                     
%                     [emd_msg,p] = get_msg(msg,p,1);
%                     if strcmp(emd_msg,'1')
%                     [Stego_block,intensity]=Generate_correspon_A(Cover_block);%1,3象限
%                     else
%                     [Stego_block,intensity]=Generate_correspon_B(Cover_block);%2,4象限
%                     end
%                     PSNR=find_psnr(Cover_block,Stego_block);
%                 else
%                     Stego_block=Cover_block;
%                 end
% %                 else %不嵌入
% %                     Stego_block=Cover_block;
% %                     if i==1&& j==2 %嵌msg_A
% %                     Stego_block=Embed_watermark(Cover_block,msg_A);
% %                     else %嵌msg_B
% %                     Stego_block=Embed_watermark(Cover_block,msg_B);
% %                     end
% %                 end
%                 %imshow(uint8(Stego_DFT_block/256))
%                 Y_stego((i-1)*N+1:i*N,(j-1)*N+1:j*N)=Stego_block;
%            end
%         end
%         PSNR_ZOOM((I-1)*ZOOM+1:I*ZOOM,(J-1)*ZOOM+1:J*ZOOM)=PSNR;
%         Intensity_ZOOM((I-1)*ZOOM+1:I*ZOOM,(J-1)*ZOOM+1:J*ZOOM)=intensity;
% %         final_stego=C_ZOOM;
% %         final_stego((i-1)*N+1:i*N,(j-1)*N+1:j*N)=Y_stego;
%         Y_stego_ZOOM((I-1)*ZOOM+1:I*ZOOM,(J-1)*ZOOM+1:J*ZOOM)=Y_stego;
%     end
% end

% find_psnr(Y_stego_ZOOM,Y_Cover)
% %% 转回RGB
% Stego=cat(3,Y_stego_ZOOM,Cover(:,:,2),Cover(:,:,3));
% Stego=ycbcr2rgb(Stego);
% %% 存起来
% imwrite(Stego,'./Marked_image/Marked_fruits24.png');
% save Stego Stego
% save p p
% save key key